package core.util;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


/**
 * redis connection pool
 * 
 * @author zhouyun
 * 
 */
public final class JedisUtil {

	/*static {
        PoolConfig poolConfig = new PoolConfig();
        poolConfig.setMaxActive(30);
        poolConfig.setMaxIdle(30);
        poolConfig.setMinIdle(5);
        poolConfig.setMaxWait(2000);
        poolConfig.setTestOnBorrow(true);

        ServerConfig serverConfig = new ServerConfig();
        serverConfig.setServerIp("192.168.157.150");
        serverConfig.setServerPort(6379);
        poolConfig.setMaxActive(PropertiesUtil.getInt("redis.maxactive"));
        poolConfig.setMaxIdle(PropertiesUtil.getInt("redis.maxidle"));
        poolConfig.setMinIdle(PropertiesUtil.getInt("redis.minidle"));
        poolConfig.setMaxWait(PropertiesUtil.getInt("redis.maxwait"));
        poolConfig.setTestOnBorrow(PropertiesUtil.getBoolean("redis.testonborrow"));

        ServerConfig serverConfig = new ServerConfig();
        serverConfig.setServerIp(PropertiesUtil.getString("redis.ip"));
        serverConfig.setServerPort(PropertiesUtil.getInt("redis.port"));
        serverConfig.setTimeout(PropertiesUtil.getLong("redis.timeout"));
        stringSingleCacheClient = StringSingleClientFactory.newInstance().initialize(poolConfig, serverConfig).createSingleClient();
        
    }*/

	
    static {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(PropertiesUtil.getInt("redis.maxactive"));
		config.setMaxIdle(PropertiesUtil.getInt("redis.maxidle"));
		config.setMaxWait(PropertiesUtil.getLong("redis.maxwait"));
		config.setTestOnBorrow(PropertiesUtil.getBoolean("redis.testonborrow"));
		
		String host = PropertiesUtil.getString("redis.host");
		String password = PropertiesUtil.getString("redis.password");
		Integer port = PropertiesUtil.getInt("redis.port");
		Integer timeout = PropertiesUtil.getInt("redis.timeout");
		
		jedisPool = new JedisPool(config, host, port, timeout, password);
    }
	
	/**
	 * Jedis Connection Pool
	 */
	private static JedisPool jedisPool;
	
	/**
	 * jedis thread local variable
	 */
	private static ThreadLocal<Jedis> jedisHolder = new ThreadLocal<Jedis>();
	
	/**
	 * get a jedis object
	 * @return
	 */
	public static Jedis getJedis(){
		Jedis jedis = jedisHolder.get();
		if (jedis == null) {
			jedis = jedisPool.getResource();
			jedisHolder.set(jedis);
		}
		return jedis;
	}
	
	/**
	 * return jedis obj instance
	 */
	public static void returnJedis(){
		Jedis jedis = jedisHolder.get();
		if (jedis != null) {
			jedisPool.returnResource(jedis);
			jedisHolder.remove();
		}
	}
	
	/**
	 * return jedis obj instance
	 */
	public static void returnBrokenResource(){
		Jedis jedis = jedisHolder.get();
		if (jedis != null) {
			jedisPool.returnBrokenResource(jedis);
			jedisHolder.remove();
		}
	}
	
}
